/**
* @file 搜索组件
* @author renzhiqiang
* @date 2017-01-20
*/
<template>
  <div>
    <ul class="mui-table-view" id='upload'>
      <li class="mui-table-view-cell theRightAddFile" v-for="item in configure">
        <template v-if="!item.fid">
          <div @tap="click($index)">
          <span class='mui-icon mui-icon-upload square'></span>
          <span class="uploadTitle" v-text="item.text"></span>
          </div>
        </template>
        <template v-else>
          <div style="line-height: 24px;" class="mui-slider-handle">
                  <span class="iconFamily fileIcons" :class="{
                 'fileTypeWork': getFileType(item.type) == 0,
                 'fileTypeExl':getFileType(item.type) == 1,
                 'fileTypePpt':getFileType(item.type) == 2,
                 'fileTypePdf':getFileType(item.type) == 3,
                 'mui-icon-addFile':getFileType(item.type) == 4}"></span>
            {{item.value}}
          </div>
          <div class="mui-slider-right mui-disabled">
            <a class="mui-btn mui-btn-red" @click="delete($index)">删除</a>
          </div>
        </template>
      </li>
    </ul>
  </div>
</template>
<style lang="less" rel="stylesheet/less">
  @import 'upload.css';
</style>
<script>
  var nativeApi = require('nativeApi');
  import {APIS} from 'configPort';
  export default{
    props: {
      configure: {} //组件配置属性
    },
    methods: {
      click: function (idx) { // 添加附件
        console.info('上传附件');
        var self = this;
        nativeApi.goAddFile({  /* 附件上传 */
          'apiJson': {
            'uploadUrl': APIS.upload_file
          },
          callback: function (result) {
            if(self.configure[idx].key == 'more'){ //提交其他附件,自动增加一栏
              self.configure.push(
               {
                fid: '',
                key: 'more',
                type: '',
                text: '提交其他附件',
                value: ''
              });
            }
            self.configure[idx].fid = result.fId;
            self.configure[idx].value = result.originName;
            self.configure[idx].type = result.extension;
          }
        });
      },
      delete: function (idx) { // 删除数据
        if(this.configure[idx].key != 'more'){
          var obj = {};
          obj = Object.assign({}, this.configure[idx], {
            fid: '',
            value: '',
            type: ''
          });
          this.configure.splice(idx, 1, obj);
        }else {
          this.configure.splice(idx,1)
        }
      },
      getFileType: function (type) {
        if (type === '.doc' || type === '.docx') {
          return 0;
        } else if (type === '.xlsx' || type === '.xls') {
          return 1;
        } else if (type === '.pptx' || type === '.ppt') {
          return 2;
        } else if (type === '.pdfx' || type === '.pdf') {
          return 3;
        } else {
          return 4;
        }
      }
    },
    created:function () {
    },
    ready: function () {
      console.info('新建');
      mui('.mui-segmented-control').scroll({
        indicators: true
      });
    }
  }
</script>
